package com.demo.connectdbtest.popoUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    //收集File
    static List<File> files = new ArrayList<>();

    /**
     * @Author：
     * @Description：获取某个目录下所有直接下级文件，不包括目录下的子目录的下的文件，所以不用递归获取
     * @Date：
     */
    public static List<File> getFilesUrl(String path) {
        File file = new File(path);
        //目录下找 其他文件绝对路径
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                files.add(tempList[i]);
                //文件名，不包含路径
                //String fileName = tempList[i].getName();
            }
            if (tempList[i].isDirectory()) {
                //这里就不递归了，
            }
        }
        return files;
    }

    //读取判断合并
    public static void ReadMerge(File file, String NewName){
        BufferedReader bufferedReader = null;

        BufferedWriter bufferedWriter = null;

        //输出URL
        String newUrl = ""+NewName;

        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            bufferedWriter = new BufferedWriter(new FileWriter(newUrl,true));

            String len = null;
            while ((len = bufferedReader.readLine())!=null){
                bufferedWriter.write(len,0,len.length());
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedWriter!=null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        //输入URL
        files = getFilesUrl("");

        for (int i = 0;i<files.size();i++){
            int tem = 0;
            File file = files.get(i);

            String[] substring = file.getName().split("_");
            tem = Integer.valueOf(substring[1]);
            String tem1 = substring[0];

            System.out.println(tem);

            ReadMerge(file,tem1+tem+".png.txt");
        }

        //TODO 名字相同情况下
        for (int i = 0;i<files.size();i++){
            int tem = 0;
            File file = files.get(i);

            //String[] substring = file.getName().split("_");

            ReadMerge(file,file.getName());
        }
    }

//    原文链接：https://blog.csdn.net/jj89929665/article/details/115871832
}
